package com.jk11a.shangdaast.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.jk11a.shangdaast.notice.NoticeBean;
import com.jk11a.shangdaast.user.LoginActivity;

public class NoticeDao {

	public List<NoticeBean> gethomework(int id){
	
		List<NoticeBean> homeworks=new ArrayList<NoticeBean>();
		try {
			String urlString = "http://10.21.239.9:8080/ShangDaAstServer/homework";
			HttpPost httpPost = new HttpPost(urlString);  
			List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();  
			 nameValuePair.add(new BasicNameValuePair("id",Integer.toString(id)));
			 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,HTTP.UTF_8));
			 HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);
			 //若状态码为200 ok
			   if (httpResponse.getStatusLine().getStatusCode() == 200) { 
				   //读返回数据
			 String retSrc = EntityUtils.toString(httpResponse.getEntity());
			 JSONObject result = new JSONObject( retSrc);  
			 JSONArray jsonArray = result.getJSONArray("homework");
			 int iSize = jsonArray.length();  
			 for (int i = 0; i < iSize; i++) {  
				 JSONObject jsonObj = jsonArray.getJSONObject(i);
					NoticeBean noticeBean=new NoticeBean();
					noticeBean.setWriterid(jsonObj.getInt("writerid"));
					noticeBean.setTitle(jsonObj.getString("title"));
					noticeBean.setMessage(jsonObj.getString("message"));
					noticeBean.setPublishtime(jsonObj.getString("publishtime"));
					homeworks.add(noticeBean);
			 }
			   }
		}catch(Exception e){
			e.printStackTrace();
		}
		return homeworks;
	}
	
	public List<NoticeBean> getweeklynotice(int id){

		List<NoticeBean> weeklynotices=new ArrayList<NoticeBean>();
		try {
			String urlString = "http://10.21.239.9:8080/ShangDaAstServer/weeklynotice";
			HttpPost httpPost = new HttpPost(urlString);  
			List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();  
			 nameValuePair.add(new BasicNameValuePair("id",Integer.toString(id)));
			 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,HTTP.UTF_8));
			 HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost); 
			 //若状态码为200 ok
			   if (httpResponse.getStatusLine().getStatusCode() == 200) { 
				   //读返回数据
			 String retSrc = EntityUtils.toString(httpResponse.getEntity());
			 Log.i("result", retSrc);
			 JSONObject result = new JSONObject(retSrc);  
			 JSONArray jsonArray = result.getJSONArray("weeklynotice");
			 int iSize = jsonArray.length();  
			 for (int i = 0; i < iSize; i++) {  
				 JSONObject jsonObj = jsonArray.getJSONObject(i);
					NoticeBean noticeBean=new NoticeBean();
					noticeBean.setTitle(jsonObj.getString("title"));
					noticeBean.setMessage(jsonObj.getString("message"));
					noticeBean.setWriterid(jsonObj.getInt("writerid"));
					noticeBean.setPublishtime(jsonObj.getString("publishtime"));
					weeklynotices.add(noticeBean);
			 }
			   }
		}catch(Exception e){
			e.printStackTrace();
		}
		return weeklynotices;
	
	}
	
	public List<NoticeBean> getcontestnotice(int id){
		List<NoticeBean> contests=new ArrayList<NoticeBean>();
		try {
			String urlString = "http://10.21.239.9:8080/ShangDaAstServer/contestnotice";
			HttpPost httpPost = new HttpPost(urlString);  
			List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();  
			 nameValuePair.add(new BasicNameValuePair("id",Integer.toString(id)));
			 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,HTTP.UTF_8));
			 HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);  
			 //若状态码为200 ok
			   if (httpResponse.getStatusLine().getStatusCode() == 200) { 
				   //读返回数据
			 String retSrc = EntityUtils.toString(httpResponse.getEntity());
			 JSONObject result = new JSONObject( retSrc);  
			 JSONArray jsonArray = result.getJSONArray("contest");
			 int iSize = jsonArray.length();  
			 for (int i = 0; i < iSize; i++) {  
				 JSONObject jsonObj = jsonArray.getJSONObject(i);
					NoticeBean noticeBean=new NoticeBean();
					noticeBean.setTitle(jsonObj.getString("title"));
					noticeBean.setMessage(jsonObj.getString("message"));
					noticeBean.setWriterid(jsonObj.getInt("writerid"));
					noticeBean.setPublishtime(jsonObj.getString("publishtime"));
					contests.add(noticeBean);
			 }
			   }
		}catch(Exception e){
			e.printStackTrace();
		}
		return contests;
	
	}
	
	public void addnotice(NoticeBean noticeBean) throws Exception{

		String urlString = "http://10.21.239.9:8080/ShangDaAstServer/addnotice";
		HttpPost httpPost = new HttpPost(urlString);  
		List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();  
		 nameValuePair.add(new BasicNameValuePair("title",noticeBean.getTitle()));
		 nameValuePair.add(new BasicNameValuePair("message",noticeBean.getMessage()));
		// nameValuePair.add(new BasicNameValuePair("publishtime",noticeBean.getPublishtime()));
		 nameValuePair.add(new BasicNameValuePair("endtime",noticeBean.getEndtime()));
		 nameValuePair.add(new BasicNameValuePair("writerid",Integer.toString(noticeBean.getWriterid())));
		 nameValuePair.add(new BasicNameValuePair("type", Integer.toString(noticeBean.getType())));
		 Log.i("title", noticeBean.getTitle());
		 Log.i("type",  Integer.toString(noticeBean.getType())+noticeBean.getEndtime());
		 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,HTTP.UTF_8));
		 HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);
	}

	public void deletenotice(NoticeBean noticeBean) throws Exception{
		String urlString = "http://10.21.239.9:8080/ShangDaAstServer/deletenotice";
		HttpPost httpPost = new HttpPost(urlString);  
		List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();  
		 nameValuePair.add(new BasicNameValuePair("id",Integer.toString(noticeBean.getId())));
		 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,HTTP.UTF_8));
		 HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);
	}
	
	public void modifynotice(NoticeBean noticeBean) throws Exception{
		String urlString = "http://10.21.239.9:8080/ShangDaAstServer/modifynotice";
		HttpPost httpPost = new HttpPost(urlString);  
		List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();  
		 nameValuePair.add(new BasicNameValuePair("id",Integer.toString(noticeBean.getId())));
		 nameValuePair.add(new BasicNameValuePair("title",noticeBean.getTitle()));
		 nameValuePair.add(new BasicNameValuePair("message",noticeBean.getMessage()));
		 nameValuePair.add(new BasicNameValuePair("publishtime",noticeBean.getPublishtime()));
		 nameValuePair.add(new BasicNameValuePair("writer",noticeBean.getWriter()));
		 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,HTTP.UTF_8));
		 HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);
	}
	
	public List<NoticeBean>getmynotice(int id)throws Exception{
		List<NoticeBean> mynotice=new ArrayList<NoticeBean>();
		String urlString = "http://10.21.239.9:8080/ShangDaAstServer/getnotice";
		HttpPost httpPost = new HttpPost(urlString);
		List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();  
		 nameValuePair.add(new BasicNameValuePair("id",Integer.toString(id)));
		 try{
		 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair,HTTP.UTF_8));
		 HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost); 
		 //若状态码为200 ok
		   if (httpResponse.getStatusLine().getStatusCode() == 200) { 
			   //读返回数据
		 String retSrc = EntityUtils.toString(httpResponse.getEntity());
		 Log.i("result", retSrc);
		 JSONObject result = new JSONObject(retSrc);  
		 JSONArray jsonArray = result.getJSONArray("mynotice");
		 int iSize = jsonArray.length();  
		 for (int i = 0; i < iSize; i++) {  
			 JSONObject jsonObj = jsonArray.getJSONObject(i);
				NoticeBean noticeBean=new NoticeBean();
				noticeBean.setTitle(jsonObj.getString("title"));
				noticeBean.setMessage(jsonObj.getString("message"));
				noticeBean.setWriterid(jsonObj.getInt("writerid"));
				noticeBean.setPublishtime(jsonObj.getString("publishtime"));
				mynotice.add(noticeBean);
		 }
		   }
	}catch(Exception e){
		e.printStackTrace();
	}
	return mynotice;
}
}
